<template>
  <div class="app-container">

    <el-row :gutter="20" class="header">
      <el-col :span="7">
        <el-input placeholder="请输入标题..." v-model="queryForm.query" clearable ></el-input>
      </el-col>
      <el-button type="primary" :icon="Search" @click="initVoteList">搜索</el-button>
    </el-row>

    <el-table :data="tableData" stripe style="width: 100%" >
        <el-table-column prop="id" label="编号"  width="80" align="center"/>
        <el-table-column prop="title" label="标题"  align="center"/>
        <el-table-column prop="openid" label="投票人" width="250" >
          <template v-slot="scope">
            <img :src="getServerUrl()+'image/weixinAvatar/'+scope.row.wxUserInfo.avatarUrl" width="50" height="50"/>&nbsp;&nbsp;{{scope.row.wxUserInfo.nickName}}
          </template>
        </el-table-column>
      <el-table-column  label="投票选项" width="100" align="center">
        <template v-slot="scope">
          <el-popover placement="left" :width="400" trigger="click" @show="getItemList(scope.row)">

          <template #reference>
            <el-button  type="success">查看</el-button>
          </template>

            <el-table :data="voteItemList" >
              <el-table-column type="index" label="序号" align="left" width="50" />
              <el-table-column  property="name" label="选项名称" />
            </el-table>

        </el-popover>
        </template>
      </el-table-column>

        <el-table-column prop="voteEndTime" label="投票截止时间" width="200" align="center"/>
        <el-table-column prop="action" label="操作" width="100" fixed="right" align="center">
        <template v-slot="scope" >
          <el-popconfirm   title="您确定要删除这条记录吗？" @confirm="handleDelete(scope.row.id)">
            <template #reference>
              <el-button  type="danger" :icon="Delete" v-if="hasAuth('business:vote:delete')"/>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        v-model:currentPage="queryForm.pageNum"
        v-model:page-size="queryForm.pageSize"
        :page-sizes="[10, 20, 30, 40]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
    />
  </div>

</template>

<script setup>
import {ref} from 'vue';
import requestUtil,{getServerUrl} from "@/util/request";
import { Search ,Delete,DocumentAdd ,Edit, Tools, RefreshRight} from '@element-plus/icons-vue'

import { ElMessage, ElMessageBox } from 'element-plus'

const tableData=ref({})

const total=ref(0)

const queryForm=ref({
  query:'',
  pageNum:1,
  pageSize:10
})

const id=ref(-1)

const voteItemList=ref([])


const initVoteList=async()=>{
  const res=await requestUtil.post("business/vote/list",queryForm.value);
  tableData.value=res.data.voteList;
  total.value=res.data.total;
}

initVoteList();

const handleSizeChange=(pageSize)=>{
  queryForm.value.pageNum=1;
  queryForm.value.pageSize=pageSize;
  initVoteList();
}

const handleCurrentChange=(pageNum)=>{
  queryForm.value.pageNum=pageNum;
  initVoteList();
}

const getItemList=(row)=>{
  console.log("row="+JSON.stringify(row))
  voteItemList.value=row.voteItemList;

}

const handleDelete=async (id)=>{

  const res=await requestUtil.get("business/vote/delete/"+id)
  if(res.data.code==200){
    ElMessage({
      type: 'success',
      message: '执行成功!'
    })
    initVoteList();
  }else{
    ElMessage({
      type: 'error',
      message: res.data.msg,
    })
  }
}

</script>

<style lang="scss" scoped>

.header{
  padding-bottom: 16px;
  box-sizing: border-box;
}

.el-pagination{
  float: right;
  padding: 20px;
  box-sizing: border-box;
}

::v-deep th.el-table__cell{
  word-break: break-word;
  background-color: #f8f8f9 !important;
  color: #515a6e;
  height: 40px;
  font-size: 13px;

}

.el-tag--small {
  margin-left: 5px;
}
</style>
